<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				sponsorCacheKey:'zyk-ss-implementationReport-sponsor-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.sysdictComboboxUrl('39318')
				]
				if(fileId){
					ajaxArr.push(
						{
							url:'implementationReport/getValue',
							param:{
								fileId:fileId
							}
						}
					)
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posName"),mainData)
					changeEditor("title7","reviewCommentId","data",configSelectData(res[1],"id","dicname"),mainData)
					changeEditor("title1","sponsorCode",{
						link:'../../public/pageSearch/pageSearch',
						linkParam: {
							other: JSON.stringify({
								
							}),
							initload: true,
							namefield:'sponsorCode',
							notefield :'proposalName',
							searchfield:'q',
							url:'saneProposalList/getDate',
							cachename: _that.sponsorCacheKey,
						}
					},null,mainData)
					let tempFileList = []
					let fileUrlList = []
					changeEditor('bottominfo','bottomFileUpload','fileUploadKey',"59",mainData)
					changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
					changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
					if(fileId){
						var fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[2].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value[index])
								_that.$set(_that.formdata,index,res[2].value[index])
							}
						}
						if(_that.formdata.bottomFileList){
							let tempFileList = []
							let fileUrlList = _that.formdata.bottomFileList.map(t=>{
								tempFileList.push(t.fileSaveName)
								return t.fileSaveName
							})
							changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
							changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
						}
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("title7","reviewCommentId")
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				_that.formdata.updateBottomFileFlag= true
				if(Object.prototype.toString.call(_that.$refs.autoform.formdata.bottomFileList) == "[object String]"){
					_that.formdata.bottomFileListStr = _that.$refs.autoform.formdata.bottomFileList|| ''
				}else{
					_that.formdata.bottomFileListStr = JSON.stringify(_that.$refs.autoform.formdata.bottomFileList || []) || ''
				}
				delete _that.formdata.bottomFileUpload
				delete _that.formdata.bottomFileList
				if(!_that.edit){
					_that.om.formSave('implementationReport/doAdd',_that.formdata,_that)
				}else{
					_that.om.formSave('implementationReport/doUpdate',_that.formdata,_that)
				}
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				};
				uni.getStorage({
					key:_that.sponsorCacheKey,
					success:(res)=>{
						if(res.data){
							let data = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.sponsorCode = data.sponsorCode || ''
							formdata.ptime = data.ptime|| ''
							formdata.sponsorName = data.sponsorName|| ''
							formdata.sponsorId = data.sponsorId || ''
							formdata.postName = data.postName|| ''
							formdata.postId= data.postId|| ''
							formdata.organizationId = data.organizationId|| ''
							formdata.organizationName = data.organizationName|| ''
							formdata.phone = data.phone|| ''
							formdata.proposalCategoryName = data.proposalCategoryName|| ''
							formdata.proposalName = data.proposalName|| ''
							formdata.expect = data.expect|| ''
							formdata.problem = data.problem|| ''
							formdata.estimatedCost = data.estimatedCost|| ''
							formdata.proposalContent = data.proposalContent|| ''
							formdata.responsibleOrganization = data.responsibleOrganization|| ''
							formdata.responsibleOrganizationId = data.responsibleOrganizationId|| ''
							formdata.organizationUserId = data.organizationUserId|| ''
							formdata.organizationUser = data.organizationUser|| ''
							formdata.implementationUserId = data.implementationUserId|| ''
							formdata.implementationUser = data.implementationUser|| ''
							formdata.completionDate = data.completionDate|| ''
							formdata.deliverables = data.deliverables|| ''
							uni.removeStorage({
								key:_that.sponsorCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
       